//+---------------------------------------------------------------------------
//
// Copyright (C) Microsoft Corporation, 2008
//
// File: DeliveryServiceHostFactory.cs
//
// Contents: Service host factory for DeliveryService
//
// Created by: Adam Wardynski (adamwa) Mar 21 2008
//
//----------------------------------------------------------------------------
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace Microsoft.Reputation.Services
{
    public sealed class MrsDeliveryServiceHost : ServiceHost
    {
        protected override void OnClosed()
        {
            using (TraceUtility.TraceMethod(Tracing.Source))
            {
                MrsDeliveryService.Shutdown();
                OverrideThreadObject.Shutdown();
                base.OnClosed();
            }
        }

        protected override void OnOpening()
        {
            using (TraceUtility.TraceMethod(Tracing.Source))
            {
                MrsDeliveryService.Initialize();
                OverrideThreadObject.Initialize();
                base.OnOpening();
            }
        }
        
        protected override void OnFaulted()
        {
            using (TraceUtility.TraceMethod(Tracing.Source))
            {
                //
                // If needed, some more logging/tracing can be done here
                //
                base.OnFaulted();
            }
        }


        public MrsDeliveryServiceHost(params Uri[] baseAddresses)
            : base(typeof(MrsDeliveryService), baseAddresses)
        {
            this.UnknownMessageReceived += OnUnknownMessageReceived;
        }

        void OnUnknownMessageReceived(object sender, UnknownMessageReceivedEventArgs e)
        {
            using (TraceUtility.TraceMethod(Tracing.Source))
            {
                //
                // If needed, some more logging/tracing can be done here
                //
            }
        }
    }

    public sealed class MrsDeliveryServiceHostFactory : ServiceHostFactory
    {
        public override ServiceHostBase CreateServiceHost(string constructorString, params Uri[] baseAddresses)
        {
            return new MrsDeliveryServiceHost(baseAddresses);
        }
    }
}
